package com.example.lay.io;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cuilei on 2014/12/9.
 */
public class MyExplorerAcvitity extends Activity {
    private ListView lvFiles;
    private EditText eTxtDir;
    private File currentParent;
    private File[] currentFiles;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_sd_explorer);
        lvFiles = (ListView) findViewById(R.id.lv_explorer);
        eTxtDir = (EditText) findViewById(R.id.eTxtDir);
        File root = new File("/mnt/sdcard");
        if (root.exists()) {
            currentParent = root;
            currentFiles = root.listFiles();
            //填充listView
            inflatListView(currentFiles);
        }
        lvFiles.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (currentFiles[position].isFile()) return;
                File[] tem = currentFiles[position].listFiles();
                if (tem == null || tem.length == 0){
                    Toast.makeText(MyExplorerAcvitity.this, "该目录为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                currentParent = currentFiles[position];
                currentFiles = tem;
                inflatListView(currentFiles);
            }
        });
        ((Button)findViewById(R.id.btnBak)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if(currentParent.getCanonicalPath().equals("/mnt/sdcard")){
                        Toast.makeText(MyExplorerAcvitity.this,"已经无法返回",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    currentParent = currentParent.getParentFile();
                    currentFiles = currentParent.listFiles();
                    inflatListView(currentFiles);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void inflatListView(File[] files) {
        List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>();
        Map<String, Object> listItem = null;
        for (int i = 0; i < files.length; i++) {
            listItem = new HashMap<String, Object>();
            if (files[i].isDirectory()) {//如果是文件夹
                listItem.put("icon", R.drawable.folder);
            } else if (files[i].isFile()) {
                listItem.put("icon", R.drawable.file);//如果是文件
            }
            listItem.put("fileName", files[i].getName());
            listItems.add(listItem);
        }

        //创建Adapter(SimpleAdapter)
        SimpleAdapter adapter = new SimpleAdapter(this, listItems,
                R.layout.simple_item,
                new String[]{"icon", "fileName"},
                new int[]{R.id.imgIcon, R.id.txtFileName});
        lvFiles.setAdapter(adapter);
        try {
            eTxtDir.setText(currentParent.getCanonicalPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
